package edu.ccut.saturn.basecomponent.database.facade;

import java.sql.Connection;

import edu.ccut.saturn.basecomponent.database.IDeleteSQLBuilder;
import edu.ccut.saturn.basecomponent.database.ISaturnSQL;
import edu.ccut.saturn.basecomponent.database.ISaturnSQLBuilder;
import edu.ccut.saturn.basecomponent.database.SaturnSQLBuilderFactory;
import edu.ccut.saturn.basecomponent.database.SaturnSQLHandler;
import edu.ccut.saturn.basecomponent.database.util.SaturnDataUtil;

public class DeleteByCondition {
	public int execute(String fullKey,String condition,Connection conn) throws Exception {
		if(condition==null){
			condition = "";
		}
		condition =  " WHERE 1=1 "+condition;
		 String dbType = SaturnDataUtil.getStandardDBType(fullKey);
		ISaturnSQLBuilder builder = SaturnSQLBuilderFactory
				.getSaturnSQLBuilder(dbType);
		if (builder != null) {
			IDeleteSQLBuilder sqlBuilder = builder
					.getDeleteSQLBuilder(ISaturnSQL.DELETE_OPERATION);
			ISaturnSQL saturnSQL = sqlBuilder.build(fullKey, condition);
			int count = SaturnSQLHandler.executeUpdate(saturnSQL,
					conn);
			return count;
		}
		return 0;
	}
}
